package cn.tedu.ivos.application.service.Impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditParam;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

// @Transactional是spring 提供的事务注解,用来管理事务行为,在对数据库操作时,
// 如果出现异常,则回滚事务,否则提交事务,要么都成功,要么都失败,保证数据库操作的原子性
@Transactional
@Service
@Slf4j
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public int saveApplication(ApplicationParam applicationParam) {
        log.debug("开始处理申请单保存业务");
        Application application = new Application();
        BeanUtils.copyProperties(applicationParam,application);
        log.debug("开始处理申请单保存业务，参数：{}",application);
        if(application.getId()==null){
            application.setCreateTime(new Date());
            application.setStatus(ApplicationStatusEnum.PENGDING.getCode());
            int rows = applicationMapper.insertApplication(application);
            //同时生成审批表单
            int i = 0;
            for (Long auditUserId : application.getAuditUserIdList()){
                AuditParam auditParam = new AuditParam();
                auditParam.setApplicationId(application.getId());
                auditParam.setAuditUserId(auditUserId);
                if(i==0){
                    auditParam.setAuditStatus(AuditStatusEnum.MY_PENGDING.getCode());
                    auditParam.setAuditSort(i);
                }else {
                    auditParam.setAuditStatus(AuditStatusEnum.PENGDING.getCode());
                    auditParam.setAuditSort(i);
                }
                auditService.saveAudit(auditParam);
                i++;
            }
            return rows;
        }else {
            application.setUpdateTime(new Date());
            int rows = applicationMapper.update(application);
            Vehicle vehicle = new Vehicle();
            vehicle.setId(application.getVehicleId());
            vehicle.setStatus("2");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
            return  rows;
        }

    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //加载每个申请单的审批人信息
        for (int i = 0; i < list.size(); i++) {
            //调用方法assignAuditNameAndId(applicationVO);
            list.set(i,assignAuditNameAndId(list.get(i)));
        }
        return list;
    }

    @Override
    public int cancelApplication(ApplicationParam applicationParam) {
        log.debug("开始处理申请单取消业务");
        Application application = new Application();
        BeanUtils.copyProperties(applicationParam,application);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        int rows = applicationMapper.update(application);
        //撤销审批单
        AuditParam auditParam = new AuditParam();
        auditParam.setApplicationId(application.getId());
        auditMapper.delete(auditParam);
        return rows;
    }

    private ApplicationVO assignAuditNameAndId(ApplicationVO applicationVO) {
        UserQuery userQuery = new UserQuery();
        //查询两次parentId的用户名字和信息分别拼接为字符串存入vo
        //1.准备空集合,分别用来保存当前申请单对应的审批人id与姓名
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2.根据当前传入的申请单id,查询此申请单对应的所有审批单
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.遍历得到每一个审批单
        log.debug("auditVOList:{}",auditVOList);
        for(int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.取出当前审批单中的审批人id存入空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据审批人id,查询此人姓名
            userQuery.setId(id);
            UserVO userVO = userMapper.selectUser(userQuery).get(0);
            auditUsernameList.add(userVO.getUsername());
//            auditUsernameList.add(auditVO.getAuditUsername());
        }
        //6.准备一个拼接工具
        StringJoiner stringJoiner = new StringJoiner(",");
        log.debug("auditUsernameList:{}",auditUsernameList);
        for (String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //7.给传入的申请单VO补全数据
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        return applicationVO;
    }
}
